package com.walkline.util.ui;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.KeypadListener;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.LabelField;

import com.walkline.app.DianpingAppConfig;
import com.walkline.dianping.inf.SingleDeal;
import com.walkline.util.BitmapMask;
import com.walkline.util.GPATools;

public class DealListStyleButtonField extends Field
{
    public static final int DRAWPOSITION_TOP = 0;
    public static final int DRAWPOSITION_BOTTOM = 1;
    public static final int DRAWPOSITION_MIDDLE = 2;
    public static final int DRAWPOSITION_SINGLE = 3;

    private static final int LISTSTYLE_ALARMCLOCK=0;
    private static final int LISTSTYLE_WORLDCLOCK=1;
    private static final int LISTSTYLE_REMINDER=2;

    private static final int CORNER_RADIUS = 12;

    private static final int HPADDING = 6; //Display.getWidth() <= 320 ? 4 : 6;
    private static final int VPADDING = 6;

    private static final boolean USING_LARGE_ICON = Display.getWidth()<640 ? false : true;

    private static int COLOR_BACKGROUND = 0xFFFFFF;
    private static int COLOR_BORDER = 0xBBBBBB;
    private static int COLOR_BACKGROUND_FOCUS = 0x186DEF;
    //private static final int COLOR_LIST_DISABLED = Color.DARKGRAY;//0x6D6D6D;//0x6A6A6A;

    //private Bitmap _leftIcon;
    private Bitmap _imageCover; //=USING_LARGE_ICON ? Bitmap.getBitmapResource("folder_large.png") : Bitmap.getBitmapResource("folder_small.png");

    private MyLabelField _labelTitle;
    //private MyLabelFieldMultiLine _labelDescription;
    private MyLabelField _labelCurrentPrice;
    private MyLabelField _labelLisPrice;
    private MyLabelField _labelPurchaseCount;

    private int _rightOffset;
    private int _leftOffset;
    private int _labelHeight;
    //private int _listStyle;

    private int _drawPosition = -1;

	private static Font FONT_TITLE;
	private static Font FONT_DESCRIPTION;
	private static Font FONT_CURRENT_PRICE;
	private static Font FONT_LISTPRICE;
	private static Font FONT_PURCHASECOUNT;
	
	private String _title = "";
	private String _description = "";
	private String _current_price = "";
	private String _list_price = "";
	private String _purchase_count = "";

    public DealListStyleButtonField(SingleDeal deal)
    {
        super(USE_ALL_WIDTH | Field.FOCUSABLE);

        String title = deal.getTitle();

        //if (deal.getBusinessesCount() > 1) {title = "[" + deal.getBusinessesCount() + "店通用] " + title;}

        _labelTitle = new MyLabelField(title);
        //_labelDescription = new MyLabelFieldMultiLine(deal.getDescription());
        _labelCurrentPrice = new MyLabelField("￥" + String.valueOf(deal.getCurrentPrice()));
        _labelLisPrice = new MyLabelField("￥" + String.valueOf(deal.getListPrice()));
        _labelPurchaseCount = new MyLabelField(deal.getPurchaseCount() + "人已购买");

        setFontSize();

        Bitmap imageToMask = Bitmap.getBitmapResource("thumb_1.jpg");
        Bitmap maskImage = Bitmap.getBitmapResource( "mask.png" );
        BitmapMask _mask = new BitmapMask( new XYEdges(2, 2, 2, 2), maskImage );
        _mask.applyMask(imageToMask);

        if (!USING_LARGE_ICON)
        {
        	imageToMask = GPATools.ResizeTransparentBitmap(imageToMask, 120, 75, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_STRETCH);
        }

		_imageCover = imageToMask;// USING_LARGE_ICON ? Bitmap.getBitmapResource("thumb_1.jpg") : Bitmap.getBitmapResource("thumb_1.jpg");
    }

    public void setDrawPosition(int drawPosition) {_drawPosition = drawPosition;}

    public void layout(int width, int height)
    {
        _leftOffset = HPADDING;
        _rightOffset = HPADDING;

        if (_labelTitle != null)
        {
            _labelTitle.layout(width - _leftOffset - _rightOffset - _imageCover.getWidth(), height);
            //_labelHeight = _labelTitle.getHeight();
        }

    	//if(_labelDescription != null)
    	//{
    	//	_labelDescription.layout(width - _leftOffset - _rightOffset - _imageCover.getWidth(), height);
    	//	_labelHeight += _labelDescription.getHeight();
    	//}

    	if (_imageCover != null)
    	{
    		//_labelHeight = Math.max(_labelHeight, _imageCover.getHeight());
    	}

    	if (_labelCurrentPrice != null)
    	{
    		_labelCurrentPrice.layout(width - _leftOffset - _rightOffset, height);
    		//_labelHeight += _labelCurrentPrice.getHeight();
    	}

    	if (_labelLisPrice != null)
    	{
    		_labelLisPrice.layout(width - _leftOffset - _rightOffset - _labelCurrentPrice.getWidth(), height);
    	}

    	if (_labelPurchaseCount != null)
    	{
    		_labelPurchaseCount.layout(width - _leftOffset - _rightOffset - _labelPurchaseCount.getWidth(), height);
    	}

    	_labelHeight = _labelTitle.getHeight() + _labelCurrentPrice.getHeight() + _labelPurchaseCount.getHeight();
    	_labelHeight = Math.max(_labelHeight, _imageCover.getHeight());
    	setExtent(width, _labelHeight + VPADDING * 2 + 1);
        //setExtent(width, _imageCover.getHeight() + VPADDING * 2 + 1); // _labelHeight + HPADDING);//+2* extraVPaddingNeeded);
    }

    public void setFontSize()
    {
  		FONT_TITLE = DianpingAppConfig.FONT_LIST_TITLE;
    	//FONT_DESCRIPTION = DianpingAppConfig.FONT_LIST_DESCRIPTION;
    	FONT_CURRENT_PRICE = DianpingAppConfig.FONT_LIST_CURRENTPRICE;
    	FONT_LISTPRICE = DianpingAppConfig.FONT_LIST_LISTPRICE;
    	FONT_PURCHASECOUNT = DianpingAppConfig.FONT_LIST_PURCHASECOUNT;

    	if (_labelTitle != null) {_labelTitle.setFont(FONT_TITLE);}
    	//if (_labelDescription != null) {_labelDescription.setFont(FONT_DESCRIPTION);}
    	if (_labelCurrentPrice != null) {_labelCurrentPrice.setFont(FONT_CURRENT_PRICE);}
    	if (_labelLisPrice != null) {_labelLisPrice.setFont(FONT_LISTPRICE);}
    	if (_labelPurchaseCount != null) {_labelPurchaseCount.setFont(FONT_PURCHASECOUNT);}
    }

    protected void paint(Graphics g)
    {
        // Cover Bitmap
   		g.drawBitmap(HPADDING, VPADDING, _imageCover.getWidth(), _imageCover.getHeight(), _imageCover, 0, 0);//(getHeight()-_imageCover.getHeight())/2, _imageCover.getWidth(), _imageCover.getHeight(), _imageCover, 0, 0);

        // Title Text
        try
        {
        	g.setFont(FONT_TITLE);
        	//g.pushRegion(_leftOffset, _imageCover.getHeight() + VPADDING * 3, _labelTitle.getWidth(), _labelTitle.getHeight(), 0, 0);
        	g.pushRegion(_leftOffset + _imageCover.getWidth() + HPADDING, VPADDING, _labelTitle.getWidth(), _labelTitle.getHeight(), 0, 0);
            _labelTitle.paint(g);
        } finally {
        	g.popContext();
        }

        // Description Text
    	//try
    	//{
    	//	g.setColor(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? Color.WHITE : Color.GRAY);
    	//	g.setFont(FONT_DESCRIPTION);
    	//	//g.pushRegion(_leftOffset, _imageCover.getHeight() + _labelTitle.getHeight() + VPADDING * 3, _labelDescription.getWidth(), _labelDescription.getHeight(),0,0);
    	//	g.pushRegion(_leftOffset + _imageCover.getWidth() + HPADDING, _labelTitle.getHeight() + VPADDING, _labelDescription.getWidth(), _labelDescription.getHeight(),0,0);
    	//	_labelDescription.paint(g);
    	//} finally {
    	//	g.popContext();
    	//}

    	//g.setColor(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? Color.WHITE : Color.BLACK);
    	//g.fillRect(1, getHeight() - _labelCurrentPrice.getHeight() - VPADDING, getWidth() - 2, 3);

        // Current Price Text
    	try
    	{
    		g.setColor(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? Color.WHITE : Color.ORANGERED);
    		g.setFont(FONT_CURRENT_PRICE);
    		//g.pushRegion(_leftOffset, getHeight() - _labelCurrentPrice.getHeight(), _labelCurrentPrice.getWidth(), _labelCurrentPrice.getHeight(),0,0);
    		g.pushRegion(_leftOffset + _imageCover.getWidth() + HPADDING, _labelTitle.getHeight() + VPADDING, _labelCurrentPrice.getWidth(), _labelCurrentPrice.getHeight(),0,0);
    		_labelCurrentPrice.paint(g);
    	} finally {
    		g.popContext();
    	}

        // List Price Text
    	try
    	{
    		g.setColor(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? Color.WHITE : Color.GRAY);
    		g.setFont(FONT_LISTPRICE);
    		//g.pushRegion(_leftOffset + _labelCurrentPrice.getWidth() + HPADDING *2, getHeight() - _labelLisPrice.getHeight() - VPADDING, _labelLisPrice.getWidth(), _labelLisPrice.getHeight(),0,0);
    		g.pushRegion(_leftOffset + _imageCover.getWidth() + _labelCurrentPrice.getWidth() + HPADDING *2, _labelTitle.getHeight() + _labelCurrentPrice.getHeight() - _labelLisPrice.getHeight(), _labelLisPrice.getWidth(), _labelLisPrice.getHeight(),0,0);
    		_labelLisPrice.paint(g);
    	} finally {
    		g.popContext();
    	}

        // Purchase Text
    	try
    	{
    		g.setColor(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? Color.WHITE : Color.GRAY);
    		g.setFont(FONT_PURCHASECOUNT);
    		//g.pushRegion(getWidth() - _rightOffset - _labelPurchaseCount.getWidth(), getHeight() - _labelPurchaseCount.getHeight() - VPADDING, _labelPurchaseCount.getWidth(), _labelPurchaseCount.getHeight(),0,0);
    		g.pushRegion(_leftOffset + _imageCover.getWidth() + HPADDING, getHeight() - _labelPurchaseCount.getHeight() - VPADDING, _labelPurchaseCount.getWidth(), _labelPurchaseCount.getHeight(),0,0);
    		
    		_labelPurchaseCount.paint(g);
    	} finally {
    		g.popContext();
    	}
    }

    protected void paintBackground(Graphics g)
    {
        if(_drawPosition < 0)
        {
            super.paintBackground(g);
            return;
        }

        int oldColour = g.getColor();

        switch(0)
        {
			case LISTSTYLE_ALARMCLOCK:
        		COLOR_BACKGROUND_FOCUS = 0x186DEF; //0xfe3434;

        		break;
			case LISTSTYLE_WORLDCLOCK:
        		COLOR_BACKGROUND_FOCUS = 0x0A9000;

        		break;
			case LISTSTYLE_REMINDER:
        		COLOR_BACKGROUND_FOCUS = 0xC72F00;

        		break;
		}

        int background = g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? COLOR_BACKGROUND_FOCUS : COLOR_BACKGROUND;

        try {
            if(_drawPosition == 0)
            {
                // Top
                g.setColor(background);
                //g.fillRoundRect(0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
                g.fillRect(0, 0, getWidth(), getHeight());
                g.setColor(COLOR_BORDER);
                //g.drawRoundRect(0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
                //g.drawRect(0, 0, getWidth(), getHeight());
                g.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
            } else if(_drawPosition == 1) {
                // Bottom 
                g.setColor(background);
                //g.fillRoundRect(0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
                g.fillRect(0, 0, getWidth(), getHeight());
                //g.setColor(COLOR_BORDER);
                //g.drawRoundRect(0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
            } else if(_drawPosition == 2) {
                // Middle
                g.setColor(background);
                //g.fillRoundRect(0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
                g.fillRect(0, 0, getWidth(), getHeight());
                //g.setColor(COLOR_BORDER);
                //g.drawRoundRect(0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
                g.setColor(COLOR_BORDER);
                g.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
            } else {
                // Single
                g.setColor(background);
                g.fillRoundRect(0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS);
                g.setColor(COLOR_BORDER);
                g.drawRoundRect(0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS);
            }
        } finally {
            g.setColor(oldColour);
        }
    }

    protected void drawFocus(Graphics g, boolean on)
    {
        if(_drawPosition < 0)
        {
        } else {
            boolean oldDrawStyleFocus = g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS);
            try {
                if(on)
                {
                	g.setColor(Color.WHITE);
                    g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true);
                }
                paintBackground(g);
                paint(g);
            } finally {
                g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus);
            }
        }
    }

    protected boolean keyChar( char character, int status, int time ) 
    {
    	switch (character)
    	{
			case Characters.ENTER:
	            clickButton();
	            return true;
        }

        return super.keyChar(character, status, time);
    }
    
    protected boolean navigationUnclick(int status, int time) 
    {
    	if ((status & KeypadListener.STATUS_FOUR_WAY) == KeypadListener.STATUS_FOUR_WAY)
    	{
    		clickButton(); 
    		return true;
    	}

    	return super.navigationClick(status, time);
    }

    protected boolean trackwheelClick(int status, int time)
    {
    	if ((status & KeypadListener.STATUS_TRACKWHEEL) == KeypadListener.STATUS_TRACKWHEEL)
    	{      
    		clickButton();    
    		return true;
    	}

    	return super.trackwheelClick(status, time);
    }

    protected boolean invokeAction(int action) 
    {
        switch(action)
        {
            case ACTION_INVOKE:
                clickButton(); 
                return true;
        }

        return super.invokeAction(action);
    }
         
    protected boolean touchEvent(TouchEvent message)
    {
        int x = message.getX( 1 );
        int y = message.getY( 1 );

        if (x < 0 || y < 0 || x > getExtent().width || y > getExtent().height) {return false;}
        
        switch (message.getEvent())
        {
            case TouchEvent.UNCLICK:
            	clickButton();
            	return true;
        }

        return super.touchEvent(message);
    }

    public void clickButton() {fieldChangeNotify(0);}

    public void setDirty(boolean dirty) {}
    public void setMuddy(boolean muddy) {}
}

class MyLabelField extends LabelField
{
    public MyLabelField(String text) {super(text, LabelField.ELLIPSIS);}

	public void layout(int width, int height) {super.layout(width, height);}   

	public void paint(Graphics g) {super.paint(g);}
}

class MyLabelFieldMultiLine extends LabelField
{
    public MyLabelFieldMultiLine(String text)
    {
    	super(text);

    	if (text.length() > 80)
    	{
    		text = text.substring(0, 77) + "...";
    		
    		setText(text);
    	}
    }

	public void layout(int width, int height) {super.layout(width, height);}   

	public void paint(Graphics g) {super.paint(g);}
}